﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon.Prompt;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class ReceiveGoodsBatchImport : EFViewCmes
    {
        public string InsertMeg = "";
        private string _msg = "";

        public ReceiveGoodsBatchImport()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (this.grid.CheckedEIRows.Count == 0)
            {
                this.ValidateMessagePopup("请选择一条或多条物资办理入库！");//弹出提示信息
                this.TempButtonOpen(true);
                return;
            }
            else
            {
                if (ValidateResultColumn())
                {
                    EIInfo eiInfo = new EIInfo();
                    eiInfo.Set("service", "ZMCMF2");
                    eiInfo.Set("method", "insert");

                    //主表新增block
                    this.BaseBlock(eiInfo);
                    //列表新增下一步block
                    this.ResultBlock(eiInfo);

                    HttpServiceAgent agent = new HttpServiceAgent();
                    agent.CallService(eiInfo, OnDoWorkCompleted);
                }
                else
                {
                    this.ValidateMessagePopup(_msg);//弹出提示信息
                    this.TempButtonOpen(true);
                    return;
                }
            }
        }

        /// <summary>
        /// 新增后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        private void LastClick(object sender, RoutedEventArgs e)
        {
            this.spWindows.Visibility = Visibility.Visible;
            this.detailsList.Visibility = Visibility.Collapsed;
            this.Commit.Visibility = Visibility.Collapsed;
            this.Last.Visibility = Visibility.Collapsed;
            this.Next.Visibility = Visibility.Visible;
            EFWindow wid = this.Parent as EFWindow;
            wid.UpdateLayout();
        }

        /// <summary>
        /// 下一步按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NextClick(object sender, RoutedEventArgs e)
        {
            if (ValidateResultField())
            {
                ValidateCode();
            }
        }

        private void ValidateCode()
        {
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCMB2");
            eiInfo.Set("method", "validateCode");
            eiInfo.Set("fdNoteDate", this.fdNoteDate.Value);
            eiInfo.Set("fdNoteCode", this.fdNoteCode.Value);

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, OnDoValidateCompleted);
        }

        /// <summary>
        /// 验证后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoValidateCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.GetStatus() != -1)
            {
                if (this.fdImportType.Value.Equals("一般入库"))
                {
                    if ((this.grid.Columns["fdInvoice"] as EFGridTextColumn) == null)
                    {
                        (this.grid.Columns["fdInvoice"] as EFColumn).eRegex = "^[a-zA-Z_0-9]{1,30}$";
                        (this.grid.Columns["fdInvoiceSum"] as EFColumn).eRegex = "((^\\d{1,10}$)|(^\\d{1,10}\\.\\d{0,5}$)|(^0\\.\\d{1,5}$))";
                    }
                    else
                    {
                        (this.grid.Columns["fdInvoice"] as EFGridTextColumn).eRegex = "^[a-zA-Z_0-9]{1,30}$";
                        (this.grid.Columns["fdInvoiceSum"] as EFGridTextColumn).eRegex = "((^\\d{1,10}$)|(^\\d{1,10}\\.\\d{0,5}$)|(^0\\.\\d{1,5}$))";
                    }
                }
                else
                {
                    if ((this.grid.Columns["fdInvoice"] as EFGridTextColumn) == null)
                    {
                        (this.grid.Columns["fdInvoice"] as EFColumn).eRegex = "";
                        (this.grid.Columns["fdInvoiceSum"] as EFColumn).eRegex = "";
                    }
                    else
                    {
                        (this.grid.Columns["fdInvoice"] as EFGridTextColumn).eRegex = "";
                        (this.grid.Columns["fdInvoiceSum"] as EFGridTextColumn).eRegex = "";
                    }
                }
                this.spWindows.Visibility = Visibility.Collapsed;
                this.detailsList.Visibility = Visibility.Visible;
                this.Commit.Visibility = Visibility.Visible;
                this.Last.Visibility = Visibility.Visible;
                this.Next.Visibility = Visibility.Collapsed;
                EFWindow wid = this.Parent as EFWindow;
                wid.UpdateLayout();
            }
            else
            {
                this.OnCommitMessagePopup(e.EIInfo);//操作失败，消息提醒
            }
        }

        /// <summary>
        /// 提示错误消息
        /// </summary>
        /// <param name="str"></param>
        private void ValidateMessagePopup(string str)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(str);
            pop.Show(6);

            return;
        }

        /// <summary>
        /// 点击"确定"后的回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }

        /// <summary>
        /// EFDatafield容器的全局合法性校验
        /// </summary>
        /// <param name="element">EFDatafield的容器控件</param>
        /// <returns>是否全部合法</returns>
        public bool ValidateResultField()
        {
            bool result = true;

            List<EFDataField> dataFields = this.GetChildObjects<EFDataField>(null);
            foreach (EFDataField field in dataFields)
            {
                if (field.eBindingName.Contains("baseBlock-0"))
                {
                    if (field.Visibility == Visibility.Visible)
                    {
                        result = field.Validate() && result;
                    }
                }
            }
            return result;
        }

        public bool ValidateResultColumn()
        {
            bool result = true;

            foreach (var row in this.grid.GetCheckedEIRows())
            {
                if (row["fdActualNum"] == null || row["fdActualNum"].Equals(""))
                {
                    result = false;
                }
                else
                {
                    if (Double.Parse(row["fdActualNum"].ToString()) > Double.Parse(row["fdValidateNum"].ToString()))
                    {
                        _msg = "入库数量不得大于验收数量！";
                        result = false;
                    }
                }

                if (row["fdSupplyName"] == null || row["fdSupplyName"].Equals(""))
                {
                    _msg = "供应商不得为空！";
                    result = false;
                }

                if (this.fdImportType.Value.Equals("一般入库"))
                {
                    if (row["fdInvoice"] == null || row["fdInvoice"].Equals(""))
                    {
                        _msg = "入库类型为一般入库，发票号不得为空！";
                        result = false;
                    }
                    if (row["fdInvoiceSum"] == null || row["fdInvoiceSum"].Equals(""))
                    {
                        _msg = "入库类型为一般入库，发票金额不得为空！";
                        result = false;
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 将新增页面中的录入信息组织成一个block
        /// </summary>
        /// <param name="eiInfo"></param>
        private void BaseBlock(EIInfo eiInfo)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.InsertGrid.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;

            EIBlock baseBlock = new EIBlock("baseBlock");
            baseBlock.SetColumns(base.GetDataFieldColumns("baseBlock"));
            baseBlock.AddRow(base.GetDataFieldValues("baseBlock"));
            eiInfo.AddBlock(baseBlock);
        }

        /// <summary>
        /// 将列表中的记录组织成一个block
        /// </summary>
        /// <param name="eiInfo"></param>
        private void ResultBlock(EIInfo eiInfo)
        {
            EIBlock resultBlock = new EIBlock("result");
            resultBlock.SetColumns(this.grid.EIBlock.GetColumns());
            resultBlock.SetRows(this.grid.GetCheckedEIRows());
            eiInfo.AddBlock(resultBlock);
        }
    }
}

